﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MES_SystemUI.Contorl
{
    public partial class PageControl : Control
    {
        public PageControl()
        {
            InitializeComponent();
            ChangePageNumber += PageControl_ChangePageNumber;
            // 控件宽高
            this.Width = 600;
            this.Height = 60;
            setbutton();
        }

        private void setbutton()
        {
            this.Controls.Clear();
            // 按钮宽高 30，50
            Point point = new Point(2, 10);
            // 用来判断 选中数字 前后页数
            bool iscenter = false;
            if (_pageNumber>5)
            {
                iscenter = true;
            }
            if (_pageNumber<_page-5)
            {
                iscenter = true;
            }
            int j = 1;
            for (int i = 0; i < _page + 2 && i<=12; i++)
            {
                Button btn = new Button();
                btn.Name = $"btn{i}";
                btn.Width = 30;
                btn.Height = 40;
                btn.Font = new Font("宋体", 9);
                btn.ForeColor = Color.Black;
                btn.BackColor = Color.White;
                btn.Location = point;
                if (i == 0)
                {
                    btn.Text = "<";
                    btn.Tag = -1;
                }
                if (i > 0 && i <= _page)
                {
                    btn.Text = $"{j}";
                    btn.Tag = j;
                    j++;
                }
                if (i == 12)
                {
                    btn.Text = ">";
                    btn.Tag = -2;
                }
                btn.Click += Btn_Click;
                this.Controls.Add(btn);
                point.X += 32;
            }
        }

        private void Btn_Click(object sender, EventArgs e)
        {
            Button b = sender as Button;
            if(int.Parse(b.Tag.ToString()) >=0)
                _pageNumber = int.Parse(b.Text);
        }

        private void PageControl_ChangePageNumber(int obj)
        {
        }
        #region 属性
        private int _page = 1;
        /// <summary>
        /// 总页数
        /// </summary>
        public int Page
        {
            get { return _page; }
            set { _page = value; setbutton(); }
        }
        private int _pageNumber = 1;
        /// <summary>
        /// 当前页数
        /// </summary>
        public int PageNumber
        {
            get { return _pageNumber; }
            set { _pageNumber = value; setbutton(); }
        }

        #endregion
        #region 事件
        event Action<int> ChangePageNumber;
        #endregion
        protected override void OnPaint(PaintEventArgs pe)
        {
            
            base.OnPaint(pe);
        }
    }
}
